<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/22
 * Time: 13:56
 */
namespace app\api\model;
use think\Controller;
use think\Model;
use think\Db;
use app\api\model\Tour;
class Search extends Model
{
    /*
     * 历史搜索
     */
    public function history($uid)
    {
        return Db::name('search_history')->where('user_id', $uid)->limit(8)->select();
    }

    /*
     * 推荐搜索
     *
     */
    public function t_search()
    {
        return Db::name('select_tj')->order('select_num', 'desc')->limit(8)->select();
    }

    /*
     * 点击搜索获取数据
     */
    public function search_data($uid, $title)
    {
        //先查询是否存在
        $cunzai = Db::name('search_history')->where('user_id', $uid)->where('search_title', $title)->find();
        $data = [
            'user_id' => $uid,
            'search_title' => $title,
            'create_time' => time(),
        ];

        if (empty($cunzai)) {
            Db::name('search_history')->insert($data);
        }
        //查询推荐表中是否有相同的
        $res = Db::name('select_tj')->where('select_title', $title)->find();
        if (!empty($res)) {
            Db::name('select_tj')->where('select_id', $res['select_id'])->setInc('select_num');
        } else {
            $data1 = [
                'select_title' => $title,
                'create_time' => time(),
            ];
            Db::name('select_tj')->insert($data1);
        }
    }

    /*
     * 删除历史搜索
     */
    public function del_history($uid)
    {
        $res =  Db::name('search_history')->where('user_id',$uid)->delete();
        if($res){
            return true;
        }else{
            return false;
        }
    }
}